package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.FragmentUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.FragmentDeviceBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkLinearLayout;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DeviceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DeviceFragment extends BaseNoModelFragment<FragmentDeviceBinding> {

    private String[] mTitles;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_device;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
    }

    @Override
    protected void initData() {
        setTabData();
    }

    private void setTabData() {
        mTitles = getResources().getStringArray(R.array.device_arr);
        fragments.add(new HomeTabFragment());
        fragments.add(new DeviceTabFragment());
        fragments.add(new SystemTabFragment());
        fragments.add(new NetworkTabFragment());
        FragmentUtils.add(getChildFragmentManager(), fragments, R.id.flFragment, 0);
        for (int i = 0; i < mTitles.length; i++) {
            mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab().setText(mTitles[i]));
        }
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tabLayout.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTabItemName);
        tv.setTextColor(Color.parseColor("#181818"));
        StkLinearLayout layout = view.findViewById(R.id.llTabItem);
        layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
        //设置TabLayout点击事件
        mDataBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTabItemName);
                tv.setTextColor(Color.parseColor("#181818"));
                StkLinearLayout layout = view.findViewById(R.id.llTabItem);
                layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
                FragmentUtils.showHide(tab.getPosition(), fragments);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTabItemName);
                tv.setTextColor(Color.parseColor("#FFFFFF"));
                StkLinearLayout layout = view.findViewById(R.id.llTabItem);
                layout.setBackground(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab, null);
        TextView text = view.findViewById(R.id.tvTabItemName);
        text.setText(mTitles[i]);
        text.setTextColor(Color.parseColor("#FFFFFF"));
        StkLinearLayout layout = view.findViewById(R.id.llTabItem);
        layout.setBackground(null);
        return view;
    }

}